﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace RenderHeads.Media.AVProVideo
{
	// Token: 0x0200001E RID: 30
	[HelpURL("http://renderheads.com/product/avpro-video/")]
	[AddComponentMenu("AVPro Video/Display uGUI", 200)]
	[ExecuteInEditMode]
	public class DisplayUGUI : MaskableGraphic
	{
		// Token: 0x060000B2 RID: 178 RVA: 0x00007A58 File Offset: 0x00005C58
		protected override void Awake()
		{
			if (DisplayUGUI._propAlphaPack == 0)
			{
				DisplayUGUI._propStereo = Shader.PropertyToID("Stereo");
				DisplayUGUI._propAlphaPack = Shader.PropertyToID("AlphaPack");
				DisplayUGUI._propVertScale = Shader.PropertyToID("_VertScale");
				DisplayUGUI._propApplyGamma = Shader.PropertyToID("_ApplyGamma");
				DisplayUGUI._propUseYpCbCr = Shader.PropertyToID("_UseYpCbCr");
				DisplayUGUI._propChromaTex = Shader.PropertyToID("_ChromaTex");
			}
			if (DisplayUGUI._shaderAlphaPacking == null)
			{
				DisplayUGUI._shaderAlphaPacking = Shader.Find("AVProVideo/UI/Transparent Packed");
				if (DisplayUGUI._shaderAlphaPacking == null)
				{
					GlobalData.Debug_LogWarning("[AVProVideo] Missing shader AVProVideo/UI/Transparent Packed");
				}
			}
			if (DisplayUGUI._shaderStereoPacking == null)
			{
				DisplayUGUI._shaderStereoPacking = Shader.Find("AVProVideo/UI/Stereo");
				if (DisplayUGUI._shaderStereoPacking == null)
				{
					GlobalData.Debug_LogWarning("[AVProVideo] Missing shader AVProVideo/UI/Stereo");
				}
			}
			base.Awake();
		}

		// Token: 0x060000B3 RID: 179 RVA: 0x00007B44 File Offset: 0x00005D44
		protected override void Start()
		{
			this._userMaterial = (this.m_Material != null);
			base.Start();
		}

		// Token: 0x060000B4 RID: 180 RVA: 0x00007B60 File Offset: 0x00005D60
		protected override void OnDestroy()
		{
			if (this._material != null)
			{
				this.material = null;
				UnityEngine.Object.Destroy(this._material);
				this._material = null;
			}
			base.OnDestroy();
		}

		// Token: 0x060000B5 RID: 181 RVA: 0x00007B94 File Offset: 0x00005D94
		private Shader GetRequiredShader()
		{
			Shader shader = null;
			StereoPacking stereoPacking = this._mediaPlayer.m_StereoPacking;
			if (stereoPacking != StereoPacking.None)
			{
				if (stereoPacking == StereoPacking.LeftRight || stereoPacking == StereoPacking.TopBottom)
				{
					shader = DisplayUGUI._shaderStereoPacking;
				}
			}
			AlphaPacking alphaPacking = this._mediaPlayer.m_AlphaPacking;
			if (alphaPacking != AlphaPacking.None)
			{
				if (alphaPacking == AlphaPacking.LeftRight || alphaPacking == AlphaPacking.TopBottom)
				{
					shader = DisplayUGUI._shaderAlphaPacking;
				}
			}
			if (shader == null && this._mediaPlayer.Info != null && QualitySettings.activeColorSpace == ColorSpace.Linear && !this._mediaPlayer.Info.PlayerSupportsLinearColorSpace())
			{
				shader = DisplayUGUI._shaderAlphaPacking;
			}
			if (shader == null && this._mediaPlayer.TextureProducer != null && this._mediaPlayer.TextureProducer.GetTextureCount() == 2)
			{
				shader = DisplayUGUI._shaderAlphaPacking;
			}
			return shader;
		}

		// Token: 0x17000010 RID: 16
		// (get) Token: 0x060000B6 RID: 182 RVA: 0x00007C88 File Offset: 0x00005E88
		public override Texture mainTexture
		{
			get
			{
				Texture result = Texture2D.whiteTexture;
				if (this.HasValidTexture())
				{
					result = this._mediaPlayer.TextureProducer.GetTexture(0);
				}
				else if (this._noDefaultDisplay)
				{
					result = null;
				}
				else if (this._defaultTexture != null)
				{
					result = this._defaultTexture;
				}
				return result;
			}
		}

		// Token: 0x060000B7 RID: 183 RVA: 0x00007CE8 File Offset: 0x00005EE8
		public bool HasValidTexture()
		{
			return this._mediaPlayer != null && this._mediaPlayer.TextureProducer != null && this._mediaPlayer.TextureProducer.GetTexture(0) != null;
		}

		// Token: 0x060000B8 RID: 184 RVA: 0x00007D28 File Offset: 0x00005F28
		private void UpdateInternalMaterial()
		{
			if (this._mediaPlayer != null)
			{
				Shader x = null;
				if (this._material != null)
				{
					x = this._material.shader;
				}
				Shader requiredShader = this.GetRequiredShader();
				if (x != requiredShader)
				{
					if (this._material != null)
					{
						this.material = null;
						UnityEngine.Object.Destroy(this._material);
						this._material = null;
					}
					if (requiredShader != null)
					{
						this._material = new Material(requiredShader);
					}
				}
				this.material = this._material;
			}
		}

		// Token: 0x060000B9 RID: 185 RVA: 0x00007DC8 File Offset: 0x00005FC8
		private void LateUpdate()
		{
			if (this._setNativeSize)
			{
				this.SetNativeSize();
			}
			if (this._lastTexture != this.mainTexture)
			{
				this._lastTexture = this.mainTexture;
				this.SetVerticesDirty();
			}
			if (this.HasValidTexture() && this.mainTexture != null && (this.mainTexture.width != this._lastWidth || this.mainTexture.height != this._lastHeight))
			{
				this._lastWidth = this.mainTexture.width;
				this._lastHeight = this.mainTexture.height;
				this.SetVerticesDirty();
			}
			if (!this._userMaterial && Application.isPlaying)
			{
				this.UpdateInternalMaterial();
			}
			if (this.material != null && this._mediaPlayer != null)
			{
				if (this.material.HasProperty(DisplayUGUI._propUseYpCbCr) && this._mediaPlayer.TextureProducer != null && this._mediaPlayer.TextureProducer.GetTextureCount() == 2)
				{
					this.material.EnableKeyword("USE_YPCBCR");
					this.material.SetTexture(DisplayUGUI._propChromaTex, this._mediaPlayer.TextureProducer.GetTexture(1));
				}
				if (this.material.HasProperty(DisplayUGUI._propAlphaPack))
				{
					Helper.SetupAlphaPackedMaterial(this.material, this._mediaPlayer.m_AlphaPacking);
					if (this._flipY && this._mediaPlayer.m_AlphaPacking != AlphaPacking.None)
					{
						this.material.SetFloat(DisplayUGUI._propVertScale, -1f);
					}
					else
					{
						this.material.SetFloat(DisplayUGUI._propVertScale, 1f);
					}
				}
				if (this.material.HasProperty(DisplayUGUI._propStereo))
				{
					Helper.SetupStereoMaterial(this.material, this._mediaPlayer.m_StereoPacking, this._mediaPlayer.m_DisplayDebugStereoColorTint);
				}
				if (this.material.HasProperty(DisplayUGUI._propApplyGamma) && this._mediaPlayer.Info != null)
				{
					Helper.SetupGammaMaterial(this.material, this._mediaPlayer.Info.PlayerSupportsLinearColorSpace());
				}
			}
			this.SetMaterialDirty();
		}

		// Token: 0x17000011 RID: 17
		// (get) Token: 0x060000BA RID: 186 RVA: 0x00008018 File Offset: 0x00006218
		// (set) Token: 0x060000BB RID: 187 RVA: 0x00008020 File Offset: 0x00006220
		public MediaPlayer CurrentMediaPlayer
		{
			get
			{
				return this._mediaPlayer;
			}
			set
			{
				if (this._mediaPlayer != value)
				{
					this._mediaPlayer = value;
					this.SetMaterialDirty();
				}
			}
		}

		// Token: 0x17000012 RID: 18
		// (get) Token: 0x060000BC RID: 188 RVA: 0x00008040 File Offset: 0x00006240
		// (set) Token: 0x060000BD RID: 189 RVA: 0x00008048 File Offset: 0x00006248
		public Rect uvRect
		{
			get
			{
				return this.m_UVRect;
			}
			set
			{
				if (this.m_UVRect == value)
				{
					return;
				}
				this.m_UVRect = value;
				this.SetVerticesDirty();
			}
		}

		// Token: 0x060000BE RID: 190 RVA: 0x0000806C File Offset: 0x0000626C
		[ContextMenu("Set Native Size")]
		public override void SetNativeSize()
		{
			Texture mainTexture = this.mainTexture;
			if (mainTexture != null)
			{
				int num = Mathf.RoundToInt((float)mainTexture.width * this.uvRect.width);
				int num2 = Mathf.RoundToInt((float)mainTexture.height * this.uvRect.height);
				if (this._mediaPlayer != null)
				{
					if (this._mediaPlayer.m_AlphaPacking == AlphaPacking.LeftRight || this._mediaPlayer.m_StereoPacking == StereoPacking.LeftRight)
					{
						num /= 2;
					}
					else if (this._mediaPlayer.m_AlphaPacking == AlphaPacking.TopBottom || this._mediaPlayer.m_StereoPacking == StereoPacking.TopBottom)
					{
						num2 /= 2;
					}
				}
				base.rectTransform.anchorMax = base.rectTransform.anchorMin;
				base.rectTransform.sizeDelta = new Vector2((float)num, (float)num2);
			}
		}

		// Token: 0x060000BF RID: 191 RVA: 0x00008154 File Offset: 0x00006354
		protected override void OnPopulateMesh(VertexHelper vh)
		{
			vh.Clear();
			this._OnFillVBO(this._vertices);
			vh.AddUIVertexStream(this._vertices, DisplayUGUI.QuadIndices);
		}

		// Token: 0x060000C0 RID: 192 RVA: 0x0000817C File Offset: 0x0000637C
		[Obsolete("This method is not called from Unity 5.2 and above")]
		protected override void OnFillVBO(List<UIVertex> vbo)
		{
			this._OnFillVBO(vbo);
		}

		// Token: 0x060000C1 RID: 193 RVA: 0x00008188 File Offset: 0x00006388
		private void _OnFillVBO(List<UIVertex> vbo)
		{
			this._flipY = false;
			if (this.HasValidTexture())
			{
				this._flipY = this._mediaPlayer.TextureProducer.RequiresVerticalFlip();
			}
			Rect uvrect = this.m_UVRect;
			Vector4 drawingDimensions = this.GetDrawingDimensions(this._scaleMode, ref uvrect);
			vbo.Clear();
			UIVertex simpleVert = UIVertex.simpleVert;
			simpleVert.color = this.color;
			simpleVert.position = new Vector2(drawingDimensions.x, drawingDimensions.y);
			simpleVert.uv0 = new Vector2(uvrect.xMin, uvrect.yMin);
			if (this._flipY)
			{
				simpleVert.uv0 = new Vector2(uvrect.xMin, 1f - uvrect.yMin);
			}
			vbo.Add(simpleVert);
			simpleVert.position = new Vector2(drawingDimensions.x, drawingDimensions.w);
			simpleVert.uv0 = new Vector2(uvrect.xMin, uvrect.yMax);
			if (this._flipY)
			{
				simpleVert.uv0 = new Vector2(uvrect.xMin, 1f - uvrect.yMax);
			}
			vbo.Add(simpleVert);
			simpleVert.position = new Vector2(drawingDimensions.z, drawingDimensions.w);
			simpleVert.uv0 = new Vector2(uvrect.xMax, uvrect.yMax);
			if (this._flipY)
			{
				simpleVert.uv0 = new Vector2(uvrect.xMax, 1f - uvrect.yMax);
			}
			vbo.Add(simpleVert);
			simpleVert.position = new Vector2(drawingDimensions.z, drawingDimensions.y);
			simpleVert.uv0 = new Vector2(uvrect.xMax, uvrect.yMin);
			if (this._flipY)
			{
				simpleVert.uv0 = new Vector2(uvrect.xMax, 1f - uvrect.yMin);
			}
			vbo.Add(simpleVert);
		}

		// Token: 0x060000C2 RID: 194 RVA: 0x000083A0 File Offset: 0x000065A0
		private Vector4 GetDrawingDimensions(ScaleMode scaleMode, ref Rect uvRect)
		{
			Vector4 zero = Vector4.zero;
			if (this.mainTexture != null)
			{
				Vector4 zero2 = Vector4.zero;
				Vector2 vector = new Vector2((float)this.mainTexture.width, (float)this.mainTexture.height);
				if (this._mediaPlayer != null)
				{
					if (this._mediaPlayer.m_AlphaPacking == AlphaPacking.LeftRight || this._mediaPlayer.m_StereoPacking == StereoPacking.LeftRight)
					{
						vector.x /= 2f;
					}
					else if (this._mediaPlayer.m_AlphaPacking == AlphaPacking.TopBottom || this._mediaPlayer.m_StereoPacking == StereoPacking.TopBottom)
					{
						vector.y /= 2f;
					}
				}
				Rect pixelAdjustedRect = base.GetPixelAdjustedRect();
				int num = Mathf.RoundToInt(vector.x);
				int num2 = Mathf.RoundToInt(vector.y);
				Vector4 vector2 = new Vector4(zero2.x / (float)num, zero2.y / (float)num2, ((float)num - zero2.z) / (float)num, ((float)num2 - zero2.w) / (float)num2);
				if (vector.sqrMagnitude > 0f)
				{
					if (scaleMode == ScaleMode.ScaleToFit)
					{
						float num3 = vector.x / vector.y;
						float num4 = pixelAdjustedRect.width / pixelAdjustedRect.height;
						if (num3 > num4)
						{
							float height = pixelAdjustedRect.height;
							pixelAdjustedRect.height = pixelAdjustedRect.width * (1f / num3);
							pixelAdjustedRect.y += (height - pixelAdjustedRect.height) * base.rectTransform.pivot.y;
						}
						else
						{
							float width = pixelAdjustedRect.width;
							pixelAdjustedRect.width = pixelAdjustedRect.height * num3;
							pixelAdjustedRect.x += (width - pixelAdjustedRect.width) * base.rectTransform.pivot.x;
						}
					}
					else if (scaleMode == ScaleMode.ScaleAndCrop)
					{
						float num5 = vector.x / vector.y;
						float num6 = pixelAdjustedRect.width / pixelAdjustedRect.height;
						if (num6 > num5)
						{
							float num7 = num5 / num6;
							uvRect = new Rect(0f, (1f - num7) * 0.5f, 1f, num7);
						}
						else
						{
							float num8 = num6 / num5;
							uvRect = new Rect(0.5f - num8 * 0.5f, 0f, num8, 1f);
						}
					}
				}
				zero = new Vector4(pixelAdjustedRect.x + pixelAdjustedRect.width * vector2.x, pixelAdjustedRect.y + pixelAdjustedRect.height * vector2.y, pixelAdjustedRect.x + pixelAdjustedRect.width * vector2.z, pixelAdjustedRect.y + pixelAdjustedRect.height * vector2.w);
			}
			return zero;
		}

		// Token: 0x040000B4 RID: 180
		[SerializeField]
		public MediaPlayer _mediaPlayer;

		// Token: 0x040000B5 RID: 181
		[SerializeField]
		public Rect m_UVRect = new Rect(0f, 0f, 1f, 1f);

		// Token: 0x040000B6 RID: 182
		[SerializeField]
		public bool _setNativeSize;

		// Token: 0x040000B7 RID: 183
		[SerializeField]
		public ScaleMode _scaleMode = ScaleMode.ScaleToFit;

		// Token: 0x040000B8 RID: 184
		[SerializeField]
		public bool _noDefaultDisplay = true;

		// Token: 0x040000B9 RID: 185
		[SerializeField]
		public bool _displayInEditor = true;

		// Token: 0x040000BA RID: 186
		[SerializeField]
		public Texture _defaultTexture;

		// Token: 0x040000BB RID: 187
		private int _lastWidth;

		// Token: 0x040000BC RID: 188
		private int _lastHeight;

		// Token: 0x040000BD RID: 189
		private bool _flipY;

		// Token: 0x040000BE RID: 190
		private Texture _lastTexture;

		// Token: 0x040000BF RID: 191
		private static Shader _shaderStereoPacking;

		// Token: 0x040000C0 RID: 192
		private static Shader _shaderAlphaPacking;

		// Token: 0x040000C1 RID: 193
		private static int _propAlphaPack;

		// Token: 0x040000C2 RID: 194
		private static int _propVertScale;

		// Token: 0x040000C3 RID: 195
		private static int _propStereo;

		// Token: 0x040000C4 RID: 196
		private static int _propApplyGamma;

		// Token: 0x040000C5 RID: 197
		private static int _propUseYpCbCr;

		// Token: 0x040000C6 RID: 198
		private const string PropChromaTexName = "_ChromaTex";

		// Token: 0x040000C7 RID: 199
		private static int _propChromaTex;

		// Token: 0x040000C8 RID: 200
		private bool _userMaterial = true;

		// Token: 0x040000C9 RID: 201
		private Material _material;

		// Token: 0x040000CA RID: 202
		private List<UIVertex> _vertices = new List<UIVertex>(4);

		// Token: 0x040000CB RID: 203
		private static List<int> QuadIndices = new List<int>(new int[]
		{
			0,
			1,
			2,
			2,
			3,
			0
		});
	}
}
